/**
 * 
 */
package any.desktop.server.resource;

import java.io.IOException;
import java.util.Iterator;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.restlet.data.CharacterSet;
import org.restlet.data.Language;
import org.restlet.data.MediaType;
import org.restlet.resource.Representation;
import org.restlet.resource.Variant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import any.restlet.JSONRepresentation;
import any.restlet.helper.RepresentationHandler;

/**
 * @author keke<keke@codehaus.org>
 * @revision $Revision: 82 $
 * @version
 */
public class SearchResourceRepresentationHandler implements
        RepresentationHandler {
    private static final String[] DOC_FIELDS = { "type", "path", "title", "id" };
    private static final Logger   LOG        = LoggerFactory
                                                     .getLogger(SearchResourceRepresentationHandler.class);

    /*
     * (non-Javadoc)
     * 
     * @see any.restlet.helper.RepresentationHandler#toEntity(org.restlet.resource.Representation)
     */
    public Object toEntity(final Representation representation)
            throws IOException {
        // TODO Auto-generated method stub
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see any.restlet.helper.RepresentationHandler#toRepresentation(java.lang.Object,
     *      org.restlet.resource.Variant)
     */

    public Representation toRepresentation(final Object entity,
            final Variant variant) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("To create a representation mediatype={}", variant
                    .getMediaType());
        }
        final MediaType mediaType = variant.getMediaType();
        if (mediaType.equals(MediaType.APPLICATION_JSON, true)) {
            final JSONObject resultSet = new JSONObject();
            final JSONArray results = new JSONArray();

            @SuppressWarnings("unchecked")
            final Iterator<Document> iter = (Iterator<Document>) entity;
            while (iter.hasNext()) {
                final Document doc = iter.next();
                final JSONObject result = new JSONObject();
                for (final String fieldName : DOC_FIELDS) {
                    final Field field = doc.getField(fieldName);
                    if (field != null) {
                        result.put(fieldName, field.stringValue());
                    }
                }
                result.put("uri", "any:desktop:item:"
                        + doc.getField("id").stringValue());
                results.add(result);
            }
            resultSet.put("results", results);
            final Representation rep = new JSONRepresentation(resultSet,
                    mediaType);
            rep.setCharacterSet(CharacterSet.UTF_8);
            rep.getLanguages().add(Language.ALL);
            return rep;
        }
        return null;
    }

}
